﻿/*
Copyright (c) 2005-2008, CellAO Team

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the CellAO Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Text;
using AO.Core;

namespace ZoneEngine.NPC
{
    /// <summary>
    /// This is a WIP class under construction (I can't stress that enough), it's NOT functional.
    /// </summary>
    class NPCScriptable : NPCType
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="c"></param>
        public override void SendToClient(Client c)
        {
            PacketWriter _writer = new PacketWriter();

            _writer.PushByte(0xDF); _writer.PushByte(0xDF);
            _writer.PushShort(10);
            _writer.PushShort(1);
            _writer.PushShort(0);
            _writer.PushInt(3086);  // sender. our server ID
            _writer.PushInt(c.Character.characterId);  // receiver
            _writer.PushInt(0x3B132D64); //our pkt id
            _writer.PushIdentity(50000, c.Character.characterId);
            _writer.PushByte(0);

            //flags
            _writer.PushBytes(new byte[] {
                //0x36, 0x0A, 0x2A, 0x4A, 0xCB,
                0x36, 0x02, 0x2B, 0x6A, 0x43, 
            });

            //0x00, 0x22, 0x8E, 0x1A, 
            _writer.PushInt(pf);
            _writer.PushCoord(coord);

            _writer.PushBytes(new byte[] {
                0x00, 0x00, 0x00, 0x00, 
                0xBF, 0x11, 0x65, 0x81, 
                0x00, 0x00, 0x00, 0x00, 
                0xBF, 0x52, 0xB3, 0xD4, 
                0x00, 0x00, 0x06, 0x28, 
            });

            _writer.PushByte((byte)(Name.Length + 1));
            _writer.PushBytes(Encoding.ASCII.GetBytes(Name));
            _writer.PushByte(0);

            _writer.PushBytes(new byte[] {
                //0x16, 0x54, 
                //0x65, 0x6C, 0x65, 0x70, 0x6F, 0x72, 0x74, 0x65,
                //0x72, 0x20, 0x54, 0x65, 0x63, 0x68, 0x6E, 0x69,
                //0x63, 0x69, 0x61, 0x6E, 0x00, 
                
                0x10, 0x88, 0x12,
                0x01, 0x00, 0x00, 0x00, 0x00, 0x89, 0x00, 0x00,
                0x00, 0x00, 0x32, 0x08, 0xDA, 0x00, 0x00, 0x03,
                0x20, 0xB1, 0x00, 0x69, 0x00, 0x1F, 0x00, 0x00,
                0x00, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
                0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
                0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
                0x00, 0x9E, 0xE8, 0xAB, 0x00, 0x00, 0x03, 0xF1,
                0x00, 0x00, 0x17, 0xA6, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x03, 0x48, 0x53, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x01, 0x00, 0x03, 0x48, 0x51,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
                0x00, 0x03, 0x48, 0x52, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x48, 0x50,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
                0x00, 0x03, 0x48, 0x54, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x0B, 0xD3, 0x00, 0x00, 0x03, 0x21,
                0x40, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
                0x00, 0x9E, 0xE8, 0x00, 0x00, 0x00, 0x00, 0x04,
                0x00, 0x00, 0x00, 0x00
            });

            byte[] reply = _writer.Finish();

            c.CmpStream.Write(reply, 0, reply.Length);
            c.CmpStream.Flush();
        }

        /// <summary>
        /// This will initiate a KnuBot conversation.
        /// </summary>
        /// <param name="c">Client to talk with</param>
        public void ConversationStart(Client c)
        {
            PacketWriter _writer = new PacketWriter();

            _writer.PushByte(0xDF); _writer.PushByte(0xDF);
            _writer.PushShort(10);
            _writer.PushShort(1);
            _writer.PushShort(0);
            _writer.PushInt(3086);  // sender. our server ID
            _writer.PushInt(c.Character.characterId);  // receiver
            _writer.PushInt(0x3B132D64); //our pkt id
            _writer.PushIdentity(50000, c.Character.characterId);
            _writer.PushByte(0);

            _writer.PushBytes(new byte[]
            {
                0x00, 0x02, 0x00, 0x00,
                0xC3, 0x50, 0x7C,
                0x40, 0x05, 0xB3, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00
            });

            byte[] reply = _writer.Finish();

            c.CmpStream.Write(reply, 0, reply.Length);
            c.CmpStream.Flush();
        }

        /// <summary>
        /// Append text to a chat session.
        /// </summary>
        /// <param name="c">Client</param>
        /// <param name="msg">The line to append</param>
        public void ConversationSendLine(Client c, string msg)
        {
            PacketWriter _writer = new PacketWriter();

            _writer.PushByte(0xDF); _writer.PushByte(0xDF);
            _writer.PushShort(10);
            _writer.PushShort(1);
            _writer.PushShort(0);
            _writer.PushInt(3086);  // sender. our server ID
            _writer.PushInt(c.Character.characterId);  // receiver
            _writer.PushInt(0x3B132D64); //our pkt id
            _writer.PushIdentity(50000, c.Character.characterId);
            _writer.PushByte(0);

            _writer.PushBytes(new byte[]
            {
                0x00, 0x02, 0x00, 0x00,
                0xC3, 0x50, 0x7C, 0x40,
                0x05, 0xB3, 0x00, 0x00,
                0x00, 0x01,
            });

            _writer.PushInt(msg.Length);
            _writer.PushBytes(Encoding.ASCII.GetBytes(msg));

            byte[] reply = _writer.Finish();
            c.CmpStream.Write(reply, 0, reply.Length);
            c.CmpStream.Flush();
        }

        /// <summary>
        /// Sends a list of available replies
        /// </summary>
        /// <param name="c">Client</param>
        /// <param name="Replies">Array of strings holding replies</param>
        public void ConversationSendReplies(Client c, string[] Replies)
        {
            PacketWriter _writer = new PacketWriter();

            _writer.PushByte(0xDF); _writer.PushByte(0xDF);
            _writer.PushShort(10);
            _writer.PushShort(1);
            _writer.PushShort(0);
            _writer.PushInt(3086);  // sender. our server ID
            _writer.PushInt(c.Character.characterId);  // receiver
            _writer.PushInt(0x55704D31); //our pkt id
            _writer.PushIdentity(50000, c.Character.characterId);
            _writer.PushByte(0);

            _writer.PushBytes(new byte[]
            {
                0x00, 0x02, 0x00, 0x00,
                0xC3, 0x50, 0x7C, 0x40,
                0x05, 0xB3, 0x00, 0x00,
                0x00,
            });

            //byte: num of replies?
            _writer.PushByte((byte)Replies.Length);

            for (int i = 0; i < Replies.Length; i++)
            {
                _writer.PushInt(Replies[i].Length);
                _writer.PushBytes(Encoding.ASCII.GetBytes(Replies[i]));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public void NPCInit(string name)
        {
            Name = name;
        }
    }
}
